package com.sys.plat.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HQLUtil {

	public static Map filter(Map filterMap, String HQL,
			String... exactMatchs) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		List<String> params = new ArrayList<String>();
		//如果参数map为空得话
		if (filterMap == null){
			retmap.put("hql", HQL);
			return retmap;
		}
		Iterator it = filterMap.keySet().iterator();
		StringBuffer buffer = new StringBuffer(" ");
		if (HQL.toLowerCase().indexOf("where") < 0) {
			buffer.append(" WHERE 1=1 ");
		}
		boolean exactMatched;
		while (it.hasNext()) {
			String key = (String) it.next();
			if ("".equals(filterMap.get(key).toString()))
				continue;
			exactMatched = false;
			buffer.append(" AND ");

			for (String exactMatch : exactMatchs) {

				if (key.equalsIgnoreCase(exactMatch)) {
					buffer.append(key);
					buffer.append("=? ");
					params.add(filterMap.get(key).toString());
					exactMatched = true;
					break;
				}
			}
			if (!exactMatched) {
				buffer.append("_t."+key);
				buffer.append(" LIKE ? ");
				params.add("%" + filterMap.get(key).toString() + "%");
			}
		}
		int orderIndx = HQL.toLowerCase().indexOf("order");
		if(orderIndx>0){
			String beforeHQL= HQL.substring(0, orderIndx);
			String endHQL=HQL.substring(orderIndx, HQL.length());
			retmap.put("hql", beforeHQL+buffer+endHQL);
		}else{
		retmap.put("hql", HQL+buffer);
		}
		retmap.put("params", params);
		return retmap;
	}
	
	public static String filterHQL(Map filterMap, String HQL,
			String... exactMatchs) {
		if (filterMap == null)
			return HQL;
		Iterator it = filterMap.keySet().iterator();
		StringBuffer buffer = new StringBuffer(" ");
		if (HQL.toLowerCase().indexOf("where") < 0) {
			buffer.append(" WHERE 1=1 ");
		}
		boolean exactMatched;
		while (it.hasNext()) {
			String key = (String) it.next();
			if ("".equals(filterMap.get(key).toString()))
				continue;
			exactMatched = false;
			buffer.append(" AND ");

			for (String exactMatch : exactMatchs) {

				if (key.equalsIgnoreCase(exactMatch)) {
					buffer.append(key);
					buffer.append("='");
					buffer.append(filterMap.get(key));
					buffer.append("' ");
					exactMatched = true;
					break;
				}
			}
			if (!exactMatched ) {
				buffer.append(key);
				buffer.append(" LIKE '%");
				buffer.append(filterMap.get(key));
				buffer.append("%' ");
			}
		}
		return HQL + buffer.toString();
	}

	/**
	 * 
	 * @param sortList
	 *            {[Property],[SortOrder]}
	 * @param query
	 * @param defaultSortOrder
	 * @return
	 */
	public static String sortHQL(List<String> sortList, String query,
			String defaultSortOrder) {
        int orderIndx = query.toLowerCase().indexOf("order");
		if (sortList == null || sortList.size() != 2)
			if (defaultSortOrder != null && orderIndx < 0)
				return query + defaultSortOrder;
			else
				return query;

		String property = sortList.get(0);
		String sortOrder = sortList.get(1);

		if(orderIndx>0){
			query=query.substring(0, orderIndx);
		}
		return new StringBuffer().append(query).append(" ORDER BY ").append(
				property).append(" ").append(sortOrder).toString();
	}
}
